package com.macro.mall.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

//@Configuration
//@EnableSwagger2
//@ComponentScan("com.macro.mall.controller")
public class SwaggersConfig implements WebMvcConfigurer {
    @Value("${swagger.enabled:true}")
    private boolean swaggerEnabled;


    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<Parameter>();
        //添加head参数start
//        ParameterBuilder firmSourceId = new ParameterBuilder();
//        firmSourceId.name("firmId")
//                .description("平台")
//                .modelRef(new ModelRef("string"))
//                .parameterType("header")
//                .required(false)
//                .defaultValue(Constant.DEFAULT_FIRM_ID)
//                .build();

        ParameterBuilder userId = new ParameterBuilder();
        userId.name("userId").description("用户ID").modelRef(new ModelRef("string")).parameterType("header").required(false).build();

        ParameterBuilder deviceId = new ParameterBuilder();
        deviceId.name("deviceId").description("设备ID").modelRef(new ModelRef("string")).parameterType("header").required(false).build();

        ParameterBuilder token = new ParameterBuilder();
        token.name("token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();

//        pars.add(firmSourceId.build());
        pars.add(userId.build());
        pars.add(token.build());
        pars.add(deviceId.build());
        //添加head参数end

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnabled)
                .select()
                //加了ApiOperation注解的类，才生成接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的类，才生成接口文档
//                .apis(RequestHandlerSelectors.basePackage("com.macro.mall.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                // .securitySchemes(security())
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("mall后台系统")
                .description("mall后台相关接口文档")
                .termsOfServiceUrl("http://www.ashy.io")
                .version("2.1")
                .build();
    }

    private List<ApiKey> security() {
        return newArrayList(
                new ApiKey("token", "token", "header"),
                new ApiKey("userId", "userId", "header"),
                new ApiKey("deviceId", "deviceId", "header"),
                new ApiKey("firmId", "firmId", "header")
        );
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(SecurityContext.builder()
                // .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$")).build());
        return securityContexts;
    }

}